Linking graphs with shiny
Linking views without shiny
library(tidyverse)
library(plotly)
library(crosstalk)
As shown in Linking views with shiny, the key attribute provides a way to attach a key (i.e., ID) to graphical elements – an essential feature when making graphical queries. When linking views in plotly outside of shiny, the suggested way to attach a key to graphical elements is via the SharedData class from the crosstalk package (Cheng 2016). At the very least, the new() method for this class requires a data frame, and a key variable. Lets suppose we’re interested in making comparisons of housing sales across cities for a given year using the txhousing dataset. Given that interest, we may want to make graphical queries that condition on a year, so we start by creating a SharedData object with year as the shared key.
sd <- SharedData$new(txhousing, ~year)
p <- ggplot(sd, aes(month, median)) +
geom_line(aes(group = year)) +
geom_smooth(data = txhousing, method = "gam") +
facet_wrap(~ city)
ggplotly(p, tooltip = "year") %>%
highlight(defaultValues = 2015, color = "red")